﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using eShop.Business;
using ECommerce.Library.Web;
using System.Collections.Generic;
using ECommerce.Library;
using System.IO;
using eShop.Business;

public partial class admin_newsletter_templates : SiteAdminBasePage
{
    private int _pageSize = 0;
    private int _totalItem = 0;
    private int _currentPageIndex = 0;
    private int _totalPage = 0;
    private bool _isLastPage = false;
    private bool _isFirstPage = false;
    private string _sortBy = string.Empty;
    private bool _showAllPage = false;
    private bool _searchUsed = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        //uccResponseMessage.Visible = false;

        if (!IsPostBack)
        {
            this._currentPageIndex = 1;
            //LoadData();
            BindGrid();
        }
    }
    private void ReadPagingData()
    {
        this._pageSize = Convert.ToInt32(ViewState["Page_Size"]);
        this._totalItem = Convert.ToInt32(ViewState["Total_Item"]);
        this._currentPageIndex = Convert.ToInt32(ViewState["Current_Page_Index"]);
        this._totalPage = Convert.ToInt32(ViewState["Total_Page"]);
        this._isLastPage = Convert.ToBoolean(ViewState["Is_Last_Page"]);
        this._isFirstPage = Convert.ToBoolean(ViewState["Is_First_Page"]);
        this._sortBy = Convert.ToString(ViewState["Sort_By"]);
        this._showAllPage = Convert.ToBoolean(ViewState["Show_All_Page"]);
        this._searchUsed = Convert.ToBoolean(ViewState["Search_Used"]);
    }

    private void WritePagingData()
    {
        ViewState["Page_Size"] = this._pageSize;
        ViewState["Total_Item"] = this._totalItem;
        ViewState["Current_Page_Index"] = this._currentPageIndex;
        ViewState["Total_Page"] = this._totalPage;
        ViewState["Is_Last_Page"] = this._isLastPage;
        ViewState["Is_First_Page"] = this._isFirstPage;
        ViewState["Sort_By"] = this._sortBy;
        ViewState["Show_All_Page"] = this._showAllPage;
        ViewState["Search_Used"] = this._searchUsed;
    }

    private void BindGrid()
    {
        grvEmailTemplate.DataSource = EmailTemplateManager.GetAllTblEmailTemplate();
        grvEmailTemplate.DataBind();
    }

    private void LoadData()
    {
        this._pageSize = SiteUtility.GridDefaultPageSize;
        this._totalItem = Newsletter.TotalNewsletterCount();
        this._totalPage = (int)Math.Ceiling(this._totalItem / (double)this._pageSize);
        this._isFirstPage = TestFirstPage();
        this._isLastPage = TestLastPage();
        this._sortBy = "Date Desc";

        //WritePagingData();
        BindGrid();
    }

    protected void Paging_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "paging")
        {
            ReadPagingData();

            if (Convert.ToString(e.CommandArgument) == "All")
            {
                this._currentPageIndex = 1;
                this._showAllPage = true;
            }
            else
            {
                int currentPage = Convert.ToInt32(e.CommandArgument);
                this._currentPageIndex = currentPage;
                this._showAllPage = false;
            }
            grvEmailTemplate.EditIndex = -1;
            WritePagingData();
            BindGrid();

        }
    }

    protected void Grid_Sort(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "sort")
        {
            grvEmailTemplate.EditIndex = -1;
            ReadPagingData();
            this._sortBy = (string)e.CommandArgument;
            WritePagingData();
            BindGrid();
        }
    }

    protected void Grid_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "view")
        {
            string id = e.CommandArgument.ToString();
            
            //string popupScript = "<script> window.open(\"" + Page.ResolveUrl(CustomSiteMap.InvoicePrintPage) + "?print=0\",\"popUpWindow\")</script>";
            string popupScript = "<script> window.open(\"" + Page.ResolveUrl(CustomSiteMap.NewsletterPrint) + "?id=" + id + "\",\"popUpWindow\")</script>";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "popup", popupScript);
        }
        
    }

    protected void Grid_Save(object sender, CommandEventArgs e)
    {
       
    }

    protected void Grid_Cancel(object sender, CommandEventArgs e)
    {
       
    }

    protected void Row_Editing(object sender, GridViewEditEventArgs e)
    {
        // Do not delete this method, it is a required dummy method
    }

    protected void Row_Edit_Cancel(object sender, GridViewCancelEditEventArgs e)
    {
        // Do not delete this method, it is a required dummy method
    }

    protected void Row_Update(object sender, GridViewUpdateEventArgs e)
    {
        // Do not delete this method, it is a required dummy method
    }

    protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      
    }

    protected void NavigateToEditPage(object sender, CommandEventArgs e)
    {
        
    }

    protected void Delete_Items(object sender, ImageClickEventArgs e)
    {
    }

    protected void Search_Click(object sender, ImageClickEventArgs e)
    {
    }

    private void BindPager()
    {
        List<string> pages = new List<string>();
        for (int i = 1; i <= this._totalPage; i++)
            pages.Add(i.ToString());

        rptPaging.DataSource = pages;
        rptPaging.DataBind();
    }

    private bool TestFirstPage()
    {
        if (this._currentPageIndex == 1)
            return true;
        else
            return false;
    }

    private bool TestLastPage()
    {
        if (this._currentPageIndex == this._totalPage)
            return true;
        else
            return false;
    }



    private int FindDataIndex(string dataId)
    {
        foreach (GridViewRow row in grvEmailTemplate.Rows)
        {
            HiddenField hdfNewsletterArchiveId = (HiddenField)row.FindControl("hdfNewsletterArchiveId");
            if (hdfNewsletterArchiveId != null && hdfNewsletterArchiveId.Value == dataId)
            {
                return row.RowIndex;
            }
        }
        return -1;
    }


}
